<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>生产调度编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
var basic_Url="${ctx}/logic/prodispatch";
var grid;
$(function(){
$("#inputForm").validate({			   
	onsubmit: true,
	 /*执行ajaxsubmit  */ 
    submitHandler: function(editform) {
            
        //alert(jsonInfo); 
        try{			    		
            url =  basic_Url+"/save.do";
            var api = frameElement.api;
        	var W = api.opener;
        
            getReturnMessage(url,"inputForm",W.searchAP);
        }catch(e){alert(e);}
    }
 }
);
$("#save").hide();
$("#tijiao").hide();
if('${prodispat.zd008Billstatus}'=="PRODISPATCH_SAVE"||'${prodispat.zd008Billstatus}'==""||'${prodispat.zd008Billstatus}'==null){
	$("#save").show();
	$("#tijiao").show();
}
grid = $("#dataGrid").ligerGrid({
    columns: [{ display: 'ID', name: 'id', width: 1 , hide:true},
              { display: '产品id', name: 'zd006MaterialsId', width: 1 , hide:true},
              { display: '<font color="gray">产品编码</font>', name: 'zd006MaterialsCode', isSort: true, width: 100},
			  { display: '<font color="gray">产品名称</font>', name: 'zd006MaterialsName', isSort: true, width: 100 },
			  { display: '<font color="gray">产品型号</font>', name: 'zd010MaterialsModel', isSort: true, width: 100},
			  { display: '<font color="gray">产品尺寸</font>', name: 'zd023MaterialsSize', isSort: true, width: 100},
			  { display: '<font color="gray">产品颜色</font>', name: 'zd026MaterialsColor', isSort: true, width: 100},
			  { display: '<font color="gray">材质</font>', name: 'zd027MaterialsQuality', isSort: true, width: 100},
			  { display: '<font color="gray">计量单位</font>', name: 'zd012MaterialsUnit', width: 60},
			  { display: '工序id', name: 'zd029Process', width: 1, hide:true},
			  { display: '<font color="gray">工序名称</font>', name: 'zd029ProcessName', isSort: true, width: 80},
        	  { display: '生产车间id', name: 'zd028WorkShop', width: 1, hide:true},
			  { display: '<font color="gray">生产车间</font>', name: 'zd028WorkShopName', isSort: true, width: 80},
			  { display: '<font color="gray">订单数量</font>', name: 'zd030PlanQuantity', width: 80},
			  //{ display: '<font color="gray">已派工数量</font>', name: 'zd016ProductionQuantity', width: 80},
			  { display: '调度数量<font color="red">*</font>', name: 'zd013Quantity', width: 80,editor : {type :'float'}},
			  { display: '单价<font color="red">*</font>', name: 'zd014Price', width: 80,editor : {type :'float'}},
			  //{ display: '', name: 'zd007UseCode', width: 80,hide:true},
			  /* { display: '工作人员<font color="red">*</font>', name: 'zd008UseName', width: 80}, */
              { display: '<font color="gray">产品描述</font>', name: 'zd021MaterialsDescription', width: 150}
              ],
              enabledEdit: true,enabledSort: true,
              usePager: true,pageSize:1000,pageSizeOptions:[1000],
              parms:[{ name : 'id' , value : '${prodispat.id}'}],
              url : basic_Url+"/griditems.do",
              width: "100%", height:'90%', checkbox: true,rownumbers:true,
              toolbar: { items: [
                                 /* { text: '选择人员', click: PersonWindow, icon: 'add' }, */
                                 { text: '复制', click: addGridRow, icon: 'add' },
                                 { text: '选择工序', click: processWindow, icon: 'add' },
                                 { line: true },
                                 { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                 ]
                       }
 });
}); 
//生产计划单列表
function PlanWindow(){
		getDG().dialog(
				{
					title : '选择生产计划单',
					width : 650,
					height : 350,
					content : 'url:' + sys_context
							+ '/public/windowalert/plan.do',    
					lock : true,
					max : true,
					min : true,
					ok : function() {
						var rows = this.content.getSelected();
						if (rows != null) {
							$(rows).each(function() {
								$("#zd004CustomerId").val(this.zd004CustomerId);
								$("#zd004CustomerCode").val(this.zd004CustomerCode);
								$("#zd004CustomerName").val(this.zd004CustomerName);
								var orderDate = this.zd005OrderDate;
								if(orderDate!=null&&orderDate!=''&&orderDate!=undefined){
									var sdate=new Date(orderDate);
									$("#zd005OrderDate").val(formatDate(sdate,"yyyy-MM-dd"));
								}
								var liveryDate = this.zd006DeliveryDate;
								if(liveryDate!=null&&liveryDate!=''&&liveryDate!=undefined){
									var ydate=new Date(liveryDate);
									$("#zd006DeliveryDate").val(formatDate(ydate,"yyyy-MM-dd"));
								}
								$("#zd007SalesMan").val(this.zd007SalesMan);
								$("#zd002OriCode").val(this.zd001FormCode);
								$("#zd002SaleId").val(this.zd002OriId);
								$("#zd003PiCode").val(this.zd003PiCode);
								var type = this.zd002OriType;
   		                		if('BT'==type){
   		                			$("#zd010Series").val("套床");
   		                		}
   		                		if('MT'==type){
   		                			$("#zd010Series").val("床垫");
   		                		}
								var url=basic_Url+"/gridBomitems.do?id="+this.id;
								multipleSearch("#dataGrid",true,"",url);
							});
						}
						this.content.Close();
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
}

//人员列表
function PersonWindow(){
	/* var mrows = grid.getCheckedRows();
	if (mrows.length == 0) {
		$.dialog.alert("请选择要调度的工序！");
		return false;
	}
		getDG().dialog(
				{
					title : '选择生产工人',
					width : 650,
					height : 350,
					content : 'url:' + sys_context
							+ '/public/windowalert/personnel.do',    
					lock : true,
					max : true,
					min : true,
					ok : function() {
						var rows = this.content.getSelected();
						if (rows != null) {
							$(mrows).each(function(i) {
								grid.updateCell('zd007UseCode', rows[0].zd001Code, mrows[i]);
								grid.updateCell('zd008UseName', rows[0].zd002Name, mrows[i]);
							});
						}
						this.content.Close();
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex(); */
}
//复制行
function addGridRow(){
	var cRows = grid.getCheckedRows();
	if(cRows.length==0){
		$.dialog.alert("请选择要复制的行！");
		return false;
	}
	var manager = $("#dataGrid").ligerGetGridManager();
	for(var i=0; i<cRows.length;i++){
		manager.addRow({
			zd006MaterialsId : cRows[i].zd006MaterialsId,
			zd006MaterialsCode : cRows[i].zd006MaterialsCode,
			zd006MaterialsName : cRows[i].zd006MaterialsName,
			zd010MaterialsModel : cRows[i].zd010MaterialsModel,
			zd023MaterialsSize : cRows[i].zd023MaterialsSize,
			zd026MaterialsColor : cRows[i].zd026MaterialsColor,
			zd027MaterialsQuality : cRows[i].zd027MaterialsQuality,
			zd012MaterialsUnit : cRows[i].zd012MaterialsUnit,
			zd029Process : '',
			zd029ProcessName : '',
			zd028WorkShop : '',
			zd028WorkShopName : '',
			zd030PlanQuantity : cRows[i].zd030PlanQuantity,
			zd014Price : cRows[i].zd014Price,
			zd021MaterialsDescription : cRows[i].zd021MaterialsDescription
		});
	}
	
}
//选择工序
function processWindow(){
	var cRows = grid.getCheckedRows();
	if(cRows==null||cRows.length==0){
		$.dialog.alert("请选择要关联的工序！");
		return false;
	}
	getDG().dialog({
		title:'选择工序',
		width : 600,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/process.do',
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var sRows = this.content.getSelected();
			if(sRows==null){
				$.dialog.alert("请选择数据");
				return false;
			}else{
				if(sRows.length>1){
					$.dialog.alert('只能选择一行数据!');
					return false;
				}else{
					var row = sRows[0];
					for(var i=0; i<cRows.length; i++){
						grid.updateCell('zd029Process', row.id, cRows[i]);
						grid.updateCell('zd029ProcessName', row.zd002Name, cRows[i]);
						grid.updateCell('zd028WorkShop', row.zd003WorkShop, cRows[i]);
						grid.updateCell('zd028WorkShopName', row.basicWorkshopDO.zd002Name, cRows[i]);
					}
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
//删除行
function deleteRow()
{ 
	grid.deleteSelectedRow();
}

function submitForm(param){
	$("#paramflag").val(param);
	var data = grid.getData();
	if (data.length == 0) {
		$.dialog.alert("您没有选择具体生产的产品，无法操作！");
		return false;
	}
	var ba = true;
	if(data !=null){ 
		for (var i = 0;i < data.length;i++){
			var row = data[i]; 
			if (undefined == row.zd014Price
					|| isNaN(row.zd014Price)
					|| row.zd014Price <= 0) {
				$.dialog.alert("第" + (i+1) + "行的工序单价没有填写");
				ba = false;
				return false;
			}
			if(undefined == row.zd013Quantity
					|| isNaN(row.zd013Quantity)
					|| row.zd013Quantity <= 0){
	           	 $.dialog.alert("您第"+(i+1)+"行的“调度数量”不合法");
	           	 ba=false;
	           	 return false;
	        }
			if(row.zd030PlanQuantity<(row.zd013Quantity+row.zd016ProductionQuantity)){
				$.dialog.alert("您第"+(i+1)+"行的“调度数量”大于“订单数量”无法调度");
	           	 ba=false;
	           	 return false;
			}
			/* if(undefined == row.zd007UseCode
					|| row.zd007UseCode==null
					|| row.zd007UseCode ==""){
				$.dialog.alert("您第"+(i+1)+"行的“生产工人”不能为空无法调度");
	           	 ba=false;
	           	 return false;
			} */
		}
	}
	jQuery.ajaxSettings.async = false;
	if (ba) {
		var i=0;
		$(data).each(function(){
			this.zd031Order=i;
			i++;
		});
		$("#itemdata").val(JSON.stringify(data));
		$("#submitFlag").val(param);
		$("#inputForm").submit();
	}
 }
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${prodispat.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${prodispat.bizVersion}"/>
<input type="hidden" id="itemdata" name="itemdata"/>
<input type="hidden" id="paramflag" name="paramflag"/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
         <table class="TableBlock" align="center">
         			<tr>
	         			<c:if test="${not empty prodispat.zd001FormCode}">
	                    <td>生产调度单号:</td>
	   		                 <td>
	   		                <input type="text" id="zd001FormCode" name="zd001FormCode" readonly="readonly" style="width: 40%;"  value="${prodispat.zd001FormCode}" />
	   		                 </td>
	   		           </c:if>
	   		           <td>产品类型:</td>
	   		           <td><input type="text" id="zd010Series" name="zd010Series"  readonly="readonly" style="width: 40%;" value="${prodispat.zd010Series }" /></td>
   		           </tr>
   		           <tr>
   		              <td >生产计划单号:</td>
   		                 <td>
   		                 <input type="hidden" id="zd002SaleId" name="zd002SaleId" value="${prodispat.zd002SaleId }"/>
   		                <input type="text" id="zd002OriCode" name="zd002OriCode" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd002OriCode}" />
   		                 <input type="button" value="..." onclick="PlanWindow();"/>
   		                 </td>
   		                  <td >PI编号:</td>
   		                 <td>
   		                <input type="text" id="zd003PiCode" name="zd003PiCode" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd003PiCode}" />
   		                </td>
   		           </tr>
                   <tr>
   		             <td >客户编码:</td>
   		                 <td>
   		                 <input type="hidden" id="zd004CustomerId" name="zd004CustomerId" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd004CustomerId}" />
   		                <input type="text" id="zd004CustomerCode" name="zd004CustomerCode" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd004CustomerCode}" />
   		                 </td>  
   		              <td >客户名称:</td>
   		                 <td>
   		                <input type="text" id="zd004CustomerName" name="zd004CustomerName" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd004CustomerName}" />
   		                 </td>     
   		           </tr>
   		           <tr>
                    	<td >订单日期:</td>
   		                 <td>
   		                	<input type="text" id="zd005OrderDate" name="zd005OrderDate" readonly="readonly" style="width: 40%;" class="required" class="Wdate" value="<fmt:formatDate value="${prodispat.zd005OrderDate}" pattern="yyyy-MM-dd" type="both" />"/>
   		                 </td>
   		                 <td >交货日期:</td>
   		                 <td>
   		                	<input type="text" id="zd006DeliveryDate" name="zd006DeliveryDate" readonly="readonly" style="width: 40%;" class="Wdate" value="<fmt:formatDate value="${prodispat.zd006DeliveryDate}" pattern="yyyy-MM-dd" type="both" />"/>
   		                 </td>
   		           </tr>
   		           <tr>
   		           	<td >业务员:</td>
   		                 <td>
   		                	<input type="text" id="zd007SalesMan" name="zd007SalesMan" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd007SalesMan}" />
   		                 </td>
   		                 <td >附件:</td>
   		           		<td>
   		           			<input type="button" value="查看附件..." onclick="uploadFileWindow('zd002SaleId','diaodu');" />
   		           		</td>
   		           </tr>
   		           <tr>
   		           	<td >备注:</td>
   		           	<td colspan="3"><input id="zd009Remark" name="zd009Remark" style="width: 40%;" value="${prodispat.zd009Remark}" /></td>
   		           </tr>
   		           <tr>
						<td colspan="4" style="margin-bottom: 10px;"><div
								id="dataGrid"></div></td>
					</tr>
                </table>               
         </div>
         <div class="formBar">
	        <ul>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm('PRODISPATCH_SAVE')">保存</button></div></div></li>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="tijiao" onclick="submitForm('PRODISPATCH_AUDIT')">提交</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
